package view;

import common.Util;
import dao.Blog;
import dao.BlogDao;
import dao.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/blogDelete")
public class BlogDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        req.setCharacterEncoding("utf-8");
        User user= Util.checkLoginStatus(req);
        if (user==null){
            String html="<h3>你尚未登录，不能删除</h3>";
            resp.getWriter().write(html);
            return;
        }
        String blogId=req.getParameter("blogId");
        if (blogId==null||"".equals(blogId)){
            String html="<h3>blogId 参数缺失！</h3>";
            resp.getWriter().write(html);
            return;
        }

        BlogDao blogDao=new BlogDao();
        Blog blog= blogDao.selectOne(Integer.parseInt(blogId));
        if (blog.getUserId()!= user.getUserId()){
            String html="<h3>你不是博客作者，不能删除</h3>";
            resp.getWriter().write(html);
            return;
        }
        blogDao.delete(Integer.parseInt(blogId));
        resp.sendRedirect("blog_list.html");
    }
}
